<script type="text/javascript">
	function editClicked() {
		javascript: edit.submit();
	}
</script>
<h1 class="center_text">Edit Profile</h1>
<hr />
<form name="edit" action="validate_edit_profile.jsp" method="post">
	<table class="center">
		<%
			// Provide default values for variables.
			if (session.getAttribute("errors") == null) {
				session.setAttribute("errors", "");
			}

			// Retrieve session variables.
			String type = (String) session.getAttribute("type");
			String user = (String) session.getAttribute("user");
			String errors = (String) session.getAttribute("errors");

			// Clear variables after reading them.
			session.setAttribute("errors", null);

			// Getting information and updating the database.
			java.sql.Connection conn = null;
			try {
				String myJDBCDriver = (String) session.getAttribute("myJDBCDriver");
				String myURL = (String) session.getAttribute("myURL");
				String myUserID = (String) session.getAttribute("myUserID");
				String myPassword = (String) session.getAttribute("myPassword");

				// Connect to the database.
				Class.forName(myJDBCDriver).newInstance();
				java.util.Properties sysprops = System.getProperties();
				sysprops.put("user", myUserID);
				sysprops.put("password", myPassword);
				conn = java.sql.DriverManager.getConnection(myURL, sysprops);

				java.sql.Statement statement = conn.createStatement();
				java.sql.ResultSet rs = statement.executeQuery("SELECT * FROM users WHERE username = '" + user + "'");
				if (rs.next()) {
					java.sql.ResultSetMetaData columns = rs.getMetaData();
					int count = columns.getColumnCount() - 4;
					if (type.equalsIgnoreCase("student")) {
						// Reviewers have less fields to edit.
						count = columns.getColumnCount();
					}
					for (int i = 3; i <= count; i++) {
						out.println("<tr><th>" + columns.getColumnName(i) + "</th>");
						out.println("<td><input type=\"text\" value=\"" + rs.getString(columns.getColumnName(i)) + "\" name =\""
								+ columns.getColumnName(i) + "\" /></td></tr>");
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				out.print(e.toString());
			} finally {
				try {
					conn.close();
				} catch (Exception ee) {
				}
			}
		%>
		<tr>
			<td></td>
			<td><a href="Javascript:void();" class="right red_button" onclick="editClicked()">Update</a></td>
		</tr>
	</table>
	<div id="errors"><%=errors%></div>
</form>